﻿Imports System
Imports System.Collections
Imports System.Xml
Imports System.IO

Public Class xml

    Structure plugins
        Dim name As String
        Dim dll As String
    End Structure


    Public Shared Function Manifest_to_array()

        Dim fileName As String = Environment.CurrentDirectory & "\plugins\manifest.xml"


        Try
            Dim arraystring As String

            '
            ' Verify the configuration file exists.
            '
            Dim ManifestFilePath As String = Environment.CurrentDirectory & "\plugins\manifest.xml"
            '
            ' Read the data
            '
            Dim xmlDoc As New XmlDocument
            Dim Nodes As XmlNodeList
            Dim Node As XmlNode
            Dim baseDataNodes As XmlNodeList
            xmlDoc.Load(ManifestFilePath)
            Nodes = xmlDoc.GetElementsByTagName("Plugins")
            For Each Node In Nodes
                baseDataNodes = Node.ChildNodes
                For Each baseDataNode As XmlNode In baseDataNodes
                    arraystring = arraystring & baseDataNode.Name & ":" & baseDataNode.InnerText & ";"
                Next
            Next

            Dim MainArr() As String = arraystring.Split(";")
            Dim pluginCount As Integer = UBound(MainArr)
            Dim PluginLines() As String

            Dim plugins(pluginCount - 1) As plugins
            For x = 0 To pluginCount - 1
                PluginLines = MainArr(x).Split(":")
                plugins(x).name = PluginLines(0)
                plugins(x).dll = PluginLines(1)
            Next



            Return plugins
        Catch ex As Exception
            Console.WriteLine(ex.Message.ToString)
            Console.ReadLine()
        End Try
    End Function

    Public Shared Sub create(ByVal plugins() As plugins)
        Dim ManifestFilePath As String = Environment.CurrentDirectory & "\plugins\manifest.xml"
        Try
            'If Not File.Exists(ManifestFilePath) Then
            '    File.Create(ManifestFilePath)
            'End If
            Dim writer As New XmlTextWriter(ManifestFilePath, System.Text.Encoding.UTF8)
            writer.WriteStartDocument(True)
            writer.Formatting = Formatting.Indented
            writer.Indentation = 2
            writer.WriteStartElement("root")
            writer.WriteStartElement("Plugins")

            For Each p In plugins
                writer.WriteStartElement(p.name)
                writer.WriteString(p.dll)
                writer.WriteEndElement()
            Next
            writer.WriteEndElement()
            writer.WriteEndElement()
            writer.WriteEndDocument()
            writer.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
